using System;
using Castle.ActiveRecord;
using Natrue.Data.FastMapping;

namespace Natrue.Data.InitHelper
{
    public class SmartEditViewInitHelper : IDisposable
    {
        private int fileIndex = 1;
        private bool created;
        private readonly SmartGroupView editView;

        public SmartEditViewInitHelper(string editViewName, string editViewCaption)
        {
            editView = new SmartGroupView {GroupViewName = editViewName, GroupViewCaption = editViewCaption};
        }

        public void AddSmartListView(string smartListViewName)
        {
            editView.Items.Add(new SmartGroupViewItem { OrderIndex = fileIndex++, SmartListViewName = smartListViewName });
        }

        public void AddSmartListView(string smartListViewName, string smartListViewCaption)
        {
            editView.Items.Add(new SmartGroupViewItem { OrderIndex = fileIndex++, SmartListViewName = smartListViewName, SmartListViewCaption = smartListViewCaption });
        }


        public void Create()
        {
            created = true;
            foreach (SmartGroupViewItem list in editView.Items)
                ActiveRecordMediator.Create(list);
            ActiveRecordMediator.Create(editView);
        }

        public void Dispose()
        {
            if (!created) Create();
        }
    }
}